/*
 * Copyright 2008 p.langer
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * 	http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package at.ac.tuwien.big.easyholdem.game;

import at.ac.tuwien.big.easyholdem.poker.Card;
import at.ac.tuwien.big.easyholdem.poker.Cards;
import at.ac.tuwien.big.easyholdem.poker.Evaluate;
import at.ac.tuwien.big.easyholdem.poker.HandRank;

/**
 * <p>
 * The result of the showdown.
 * </p>
 * 
 * <p>
 * Created on 16.01.2008 at 23:37:32.
 * </p>
 * 
 * @author Philip Langer <langer[at]big[dot]tuwien[dot]ac[dot]at>
 */
public class ShowdownResult {

	private HandRank playersHandRank;
	private HandRank dealersHandRank;

	/*
	 * The card fields.
	 */
	private Cards playersCards;
	private Cards dealersCards;
	private Cards flopCards;
	private Card turnCard;
	private Card riverCard;

	/**
	 * Creates a new {@link ShowdownResult} with the specified
	 * <code>palyersCards</code>, <code>dealersCards</code>,
	 * <code>flopCards</code>, <code>turnCard</code> and
	 * <code>riverCard</code>.
	 * 
	 * @param playersCards
	 *            the hole cards of the player.
	 * @param dealersCards
	 *            the hole cards of the dealer.
	 * @param flopCards
	 *            the flop cards.
	 * @param turnCard
	 *            the turn card.
	 * @param riverCard
	 *            the river card.
	 */
	protected ShowdownResult(Cards playersCards, Cards dealersCards,
			Cards flopCards, Card turnCard, Card riverCard) {
		super();
		this.playersCards = playersCards;
		this.dealersCards = dealersCards;
		this.flopCards = flopCards;
		this.turnCard = turnCard;
		this.riverCard = riverCard;

		// create hand ranks
		if ((this.flopCards != null && this.flopCards.size() > 0)
				&& (this.turnCard != null) && (this.turnCard != null)) {
			Cards board = new Cards(5);
			board.addAll(this.flopCards);
			board.add(this.turnCard);
			board.add(this.riverCard);
			playersHandRank = Evaluate.holdem(this.playersCards, board);
			dealersHandRank = Evaluate.holdem(this.dealersCards, board);
		}
	}

	/**
	 * Sets the players hole cards.
	 * 
	 * @param playersCards
	 *            the players cards to set
	 */
	protected void setPlayersCards(Cards playersCards) {
		this.playersCards = playersCards;
	}

	/**
	 * Sets the dealers hole cards.
	 * 
	 * @param dealersCards
	 *            the dealers cards to set
	 */
	protected void setDealersCards(Cards dealersCards) {
		this.dealersCards = dealersCards;
	}

	/**
	 * Sets the flop cards.
	 * 
	 * @param flopCards
	 *            the flop cards to set
	 */
	protected void setFlopCards(Cards flopCards) {
		this.flopCards = flopCards;
	}

	/**
	 * Sets the turn card.
	 * 
	 * @param turnCard
	 *            the turn card to set
	 */
	protected void setTurnCard(Card turnCard) {
		this.turnCard = turnCard;
	}

	/**
	 * Sets the river card.
	 * 
	 * @param riverCard
	 *            the river card to set
	 */
	protected void setRiverCard(Card riverCard) {
		this.riverCard = riverCard;
	}

	/**
	 * The player's hole cards.
	 * 
	 * @return the player's cards.
	 */
	public Cards getPlayersCards() {
		return playersCards;
	}

	/**
	 * The dealer's hole cards.
	 * 
	 * @return the dealers cards.
	 */
	public Cards getDealersCards() {
		return dealersCards;
	}

	/**
	 * The flop cards.
	 * 
	 * @return the flop cards.
	 */
	public Cards getFlopCards() {
		return flopCards;
	}

	/**
	 * The turn card.
	 * 
	 * @return the turn card.
	 */
	public Card getTurnCard() {
		return turnCard;
	}

	/**
	 * The river card.
	 * 
	 * @return the river card.
	 */
	public Card getRiverCard() {
		return riverCard;
	}

	/**
	 * The player's hand rank.
	 * 
	 * @return the player's hand rank.
	 */
	public HandRank getPlayersHandRank() {
		return playersHandRank;
	}

	/**
	 * The dealer's hand rank.
	 * 
	 * @return the dealer's hand rank.
	 */
	public HandRank getDealersHandRank() {
		return dealersHandRank;
	}

	/**
	 * Returns <code>true</code> if the player's cards are better than the
	 * dealer's cards. <code>false</code> otherwise.
	 * 
	 * @return <code>true</code> if the player's cards are better than the
	 *         dealer's cards. <code>false</code> otherwise.
	 */
	public boolean hasPlayerWon() {
		if (playersHandRank != null) {
			return playersHandRank.compareTo(dealersHandRank) > 0;
		} else {
			return false;
		}
	}

	/**
	 * Returns <code>true</code> if the player's cards are equal to the
	 * dealer's cards. <code>false</code> otherwise.
	 * 
	 * @return <code>true</code> if the player's cards are equal to the
	 *         dealer's cards. <code>false</code> otherwise.
	 */
	public boolean isSplitPot() {
		if (playersHandRank != null) {
			return playersHandRank.compareTo(dealersHandRank) == 0;
		} else {
			return false;
		}
	}

}
